home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / DGEMatrix.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  5KB  |  164 lines

  1. #ifndef DGEMATRIX_H
  2. #define DGEMATRIX_H
  3. #pragma once 
  4.  
  5. /*
  6.  *    Declarations for double precision general matricies.
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)DGEMatrix.h    2.1    8/18/89
  27.  */
  28.  
  29. /*
  30.  *    This class is derived from class DoubleVec.  Data is stored
  31.  *    FORTRAN style: by columns.
  32.  *
  33.  *    Defining the preprocessor directive "BOUNDS_CHECK" will invoke
  34.  *    bounds checking.
  35.  */
  36.  
  37. #include "DoubleVec.h"
  38.  
  39. class DGEMatrix : public DoubleVec {
  40.   int ncols;            // Number of columns
  41.   int nrows;            // Number of rows
  42. protected:
  43.   void             assertColRange(int);
  44.   void             assertRowRange(int);
  45.   void            assertRowCol(const DGEMatrix&);
  46.   void            assertLength(const DoubleVec&);
  47.   void            assertSquare();
  48.   void            assertProduct(const DGEMatrix&);
  49.   void            assertProduct(const DoubleVec&);
  50. public:
  51.   DGEMatrix();
  52.   DGEMatrix(int rows, int cols);
  53.   DGEMatrix(int rows, int cols, double initval);
  54.   DGEMatrix(const double* dat, int, int);  // Copy of dat will be made
  55.   DGEMatrix(const DoubleVec& v, int, int); // Reference to v will be made
  56.   DGEMatrix(const DGEMatrix& m);       // Reference to m will be made
  57.  
  58.   double*        data()        {return DoubleVec::data();}
  59.   int            cols();
  60.   int            rows();
  61.  
  62.   DGEMatrix&        reference(DGEMatrix& m); // Reference self to m
  63.   DGEMatrix        deepCopy();    // copy of self with distinct instance variables 
  64.   DGEMatrix        copy()        {return deepCopy();} // Synonym for deepCopy()
  65.   void            deepenShallowCopy();    // Guarantee that references==1:
  66.  
  67.   DoubleVec        operator[](int j);    // Return a col as a slice
  68.   DoubleVec        col(int j);        // Return a col as a slice
  69.   DoubleVec        row(int i);        // Return a row as a slice
  70.   DoubleVec        diagonal(int idiag=0);    // Return a diagonal as a slice
  71.   double&        operator()(int i, int j); // Subscripting
  72.  
  73. // Math functions
  74.   DGEMatrix        product(const DGEMatrix&); // Inner product
  75.   DoubleVec        product(const DoubleVec&);
  76.  
  77. // Assignment operators --- self must be same size as m
  78.   DGEMatrix&        operator=(const DGEMatrix& m);
  79.   DGEMatrix&        operator=(double);
  80.   DGEMatrix&        operator+=(const DGEMatrix& m);
  81.   DGEMatrix&        operator+=(double);
  82.   DGEMatrix&        operator-=(const DGEMatrix& m);
  83.   DGEMatrix&        operator-=(double);
  84.   DGEMatrix&        operator*=(const DGEMatrix& m);
  85.   DGEMatrix&        operator*=(double);
  86.   DGEMatrix&        operator/=(const DGEMatrix& m);
  87.   DGEMatrix&        operator/=(double);
  88.  
  89. // Increment/decrement operators
  90.   DGEMatrix&        operator++();
  91.   DGEMatrix&        operator--();
  92.  
  93. // Friendly arithmetic operators; Notice that operator* is an element-by-
  94. // element multiply, NOT a matrix multiply.
  95.   friend DGEMatrix    operator-(const DGEMatrix&);    // Unary minus
  96.   friend DGEMatrix    operator+(const DGEMatrix&);    // Unary plus
  97.   friend DGEMatrix    operator*(const DGEMatrix&, const DGEMatrix&);
  98.   friend DGEMatrix    operator/(const DGEMatrix&, const DGEMatrix&);
  99.   friend DGEMatrix    operator+(const DGEMatrix&, const DGEMatrix&);
  100.   friend DGEMatrix    operator-(const DGEMatrix&, const DGEMatrix&);
  101.   friend DGEMatrix    operator*(const DGEMatrix&, double);
  102.   friend DGEMatrix    operator*(double, const DGEMatrix&);
  103.   friend DGEMatrix    operator/(const DGEMatrix&, double);
  104.   friend DGEMatrix    operator/(double, const DGEMatrix&);
  105.   friend DGEMatrix    operator+(const DGEMatrix&, double);
  106.   friend DGEMatrix    operator+(double, const DGEMatrix&);
  107.   friend DGEMatrix    operator-(const DGEMatrix&, double);
  108.   friend DGEMatrix    operator-(double, const DGEMatrix&);
  109.  
  110. };
  111.  
  112. // Other (related) declarations:
  113. ostream&        operator<<(ostream& s, const DGEMatrix& m);
  114. DGEMatrix        transpose(const DGEMatrix&);
  115.  
  116. /******************* I N L I N E S **************************/
  117.  
  118. Inline int DGEMatrix::cols() { return ncols;}
  119. Inline int DGEMatrix::rows() { return nrows;}
  120. Inline void DGEMatrix::deepenShallowCopy(){DoubleVec::deepenShallowCopy();}
  121. Inline DGEMatrix operator+(const DGEMatrix& m)        { return m; }
  122. Inline DGEMatrix operator*(double d, const DGEMatrix& m){ return m*d; }
  123. Inline DGEMatrix operator+(double d, const DGEMatrix& m){ return m+d; }
  124.  
  125. // Return a column
  126. Inline DoubleVec DGEMatrix::operator[](int j){
  127. #if BOUNDS_CHECK
  128.   assertColRange(j);
  129. #endif
  130.   return DoubleVec::slice(j*nrows,nrows,1);
  131. }
  132.  
  133. Inline DoubleVec DGEMatrix::col(int j){    // Same as above
  134. #if BOUNDS_CHECK
  135.   assertColRange(j);
  136. #endif
  137.   return DoubleVec::slice(j*nrows,nrows,1);
  138. }
  139.  
  140. Inline DoubleVec DGEMatrix::row(int i){
  141. #if BOUNDS_CHECK
  142.   assertRowRange(i);
  143. #endif
  144.   return DoubleVec::slice(i, ncols, nrows);
  145. }
  146.  
  147. Inline DoubleVec DGEMatrix::diagonal(int i){
  148.   register int iabs=abs(i);
  149. #if BOUNDS_CHECK
  150.   assertSquare();
  151.   assertRowRange(iabs);
  152. #endif
  153.   return DoubleVec::slice(i>0 ? i*nrows : iabs, nrows-iabs, nrows+1);
  154. }
  155.  
  156. Inline double& DGEMatrix::operator()(int i, int j){
  157. #if BOUNDS_CHECK
  158.   assertRowRange(i); assertColRange(j);
  159. #endif
  160.   return DoubleVec::operator()(j*nrows+i);
  161. }
  162.  
  163. #endif DGEMATRIX_HXX
  164.